/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.audit.service.impl;

import com.koron.audit.domain.WorkFlowDepartment;
import com.koron.audit.domain.WorkFlowStaffBean;
import com.koron.audit.service.api.WorkFlowDeptAndStaffService;
import com.koron.common.bean.DepartmentTreeBean;
import com.koron.common.bean.StaffBean;
import com.koron.common.web.Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.TreeSet;

@Service
public class WorkFlowDeptAndStaffServiceImpl implements WorkFlowDeptAndStaffService {

    @Autowired
    private Util util;


    @Override
    public List<WorkFlowDepartment> toWorkFlowDepartmentList() {
        List<WorkFlowDepartment> list=new ArrayList<>();
        //返回组织架构树的平铺结构；如根节点A，子节点B,C;B的子节点为D,E;D的节点为F;平铺输出为ABDEFC;
        List<DepartmentTreeBean> departmentTreeBeanList= (List<DepartmentTreeBean>) util.getOrgTree().getData();
        if (null!=departmentTreeBeanList&&departmentTreeBeanList.size()>0){

            //比较器，用来进行排序，去重
            TreeSet<DepartmentTreeBean> selectedSet = new TreeSet<>((o1, o2) -> {
                long dist = o1.getSeq() - o2.getSeq();
                return dist > 0 ? 1 : dist == 0 ? 0 : -1;
            });
            selectedSet.addAll(departmentTreeBeanList);
            //定义一个临时的堆栈
            Stack<DepartmentTreeBean> stack = new Stack<>();
            for(DepartmentTreeBean departmentTreeBean:selectedSet){
                if (stack.isEmpty()){
                    WorkFlowDepartment department=new WorkFlowDepartment();
                    department.setId(departmentTreeBean.getDepartmentCode());
                    department.setShortname(departmentTreeBean.getName());
                    department.setName(departmentTreeBean.getName());
                    department.setCode(departmentTreeBean.getDepartmentCode());
                    department.setParentId(null);
                    department.setParentCode(null);
                    stack.push(departmentTreeBean);
                    list.add(department);
                }else{
                    DepartmentTreeBean top = stack.peek();
                    //子节点的parentMask一定比父级节点的parentMask大，同级节点parentMask相等
                    while (!stack.isEmpty() && departmentTreeBean.getParentMask()<= top.getParentMask()) {
                        stack.pop();
                        if (!stack.isEmpty())
                            top = stack.peek();
                    }
                    WorkFlowDepartment department=new WorkFlowDepartment();
                    department.setId(departmentTreeBean.getDepartmentCode());
                    department.setShortname(departmentTreeBean.getName());
                    department.setName(departmentTreeBean.getName());
                    department.setCode(departmentTreeBean.getDepartmentCode());
                    department.setParentId(top.getDepartmentCode());
                    department.setParentCode(top.getDepartmentCode());
                    list.add(department);
                    stack.push(departmentTreeBean);
                }
            }
        }
        return list;
    }

    @Override
    public List<WorkFlowStaffBean> toWorkFlowStaffList() {
        List<WorkFlowStaffBean> staffBeanList=new ArrayList<>();
        List<DepartmentTreeBean> departmentTreeBeanList= (List<DepartmentTreeBean>) util.getOrgTree().getData();
        if (null!=departmentTreeBeanList&&departmentTreeBeanList.size()>0){
            for(DepartmentTreeBean departmentTreeBean:departmentTreeBeanList){
                List<StaffBean> staffBeans= (List<StaffBean>) util.depstaff(departmentTreeBean.getDepartmentCode()).getData();
                for(StaffBean staffBean:staffBeans){
                    WorkFlowStaffBean workFlowStaffBean=new WorkFlowStaffBean();
                    BeanUtils.copyProperties(staffBean,workFlowStaffBean);
                    workFlowStaffBean.setPhotourl(staffBean.getPhoto());
                    workFlowStaffBean.setStatus(staffBean.getStatus());
                    workFlowStaffBean.setWeighting(staffBean.getWeight());
                    workFlowStaffBean.setDepartmentid(departmentTreeBean.getDepartmentCode());
                    workFlowStaffBean.setDepartmentCode(departmentTreeBean.getDepartmentCode());
                    workFlowStaffBean.setDepartmentName(departmentTreeBean.getName());
                    workFlowStaffBean.setId(staffBean.getId().toString());
                    workFlowStaffBean.setUserid(null);
                    workFlowStaffBean.setAccount(staffBean.getUserid());
                    workFlowStaffBean.setCode(staffBean.getUserid());
                    staffBeanList.add(workFlowStaffBean);
                }
            }
        }
        return staffBeanList;
    }
}
